package com.os.sys.controller;

import com.os.sys.config.MyProps;
import com.os.sys.mapper.RoleMenuMapper;
import com.os.sys.mapper.SysMenuMapper;
import com.os.sys.mapper.SysRoleMapper;
import com.os.sys.pojo.RoleMenu;
import com.os.sys.service.SysMenuService;
import com.os.sys.service.SysRoleService;
import com.os.sys.service.SysUserService;
import entity.Result;
import entity.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/roleMenu")
@RefreshScope
@Slf4j
public class RoleMenuController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private MyProps myProps;
    //https://www.cnblogs.com/superfj/p/9232482.html
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    HttpServletRequest request;
    @Autowired
    SysMenuMapper sysMenuMapper ;
    @Autowired
    RoleMenuMapper roleMenuMapper ;
    @Autowired
    SysRoleMapper sysRoleMapper ;


    @RequestMapping(value="/add",method = RequestMethod.POST)
    public Result add(@RequestBody List<RoleMenu> roleMenuList){
        log.info("roleMenuList", roleMenuList);
        if(!CollectionUtils.isEmpty(roleMenuList)){
            roleMenuMapper.deleteByRoleId(roleMenuList.get(0).getRole_id());
            for (RoleMenu roleMenu: roleMenuList) {
                RoleMenu rm = roleMenuMapper.selectByRoleIdMenuId(roleMenu.getRole_id(), roleMenu.getMenu_id());
                if(rm == null){
                    roleMenuMapper.insert(roleMenu);
                }
            }
        }
        return new Result(true, StatusCode.OK,"保存成功", null);
    }

}
